clear
set maxvar 10000

** Panel profile runs from 2012 to 2016

** We want 2012, 2014 (for Multiple imputation), and 2016

use data/taps_data2012.dta, clear

keep WUSTLID PARTYID* GRPAFFAS2 GRPAFFBS2 GRPAFFJS2 GRPAFFKS2 GRPAFF*_A GRPAFF*_B GRPAFF*_J GRPAFF*_K IDEOL1* VOTE2012PS12 APPRPRE*

rename WUSTLID wustlid

merge 1:1 wustlid using "Complete Profiles Sorted.dta", keepusing(ppgender ppethm ppeducat ppmsacat ppstaten ppreg4 ppnet income6 ppagect4 hhzip agesp religattendsp AGE_2016 NOV2012VOTE IDEOL1SP PARTYID1SP PARTYID2SP PARTYID3SP PARTYID4SP wt1SP basewtSP wtSP)
drop if _merge==2
rename _merge panel2012
recode panel2012 3=1

tab NOV2012VOTE


save "data/merge2012.dta", replace
clear

use wustlid PARTYID* APPRPRESS*  using "taps_2014release.dta"

save "merge2014.dta", replace


use "taps_2016.dta", clear

keep wustlid PARTYID* APPRPRES* VOTE2016PS*

merge 1:1 wustlid using "merge2012.dta"
gen merge2012 = _merge
drop _merge
merge 1:1 wustlid using "merge2014.dta"
gen merge2014 = _merge
drop _merge

merge 1:1 wustlid using "Complete Profiles Sorted.dta", keepusing(ppgender ppethm ppeducat ppmsacat ppstaten ppreg4 ppnet income6 ppagect4 hhzip agesp religattendsp AGE_2016 NOV2012VOTE IDEOL1SP PARTYID1SP PARTYID2SP PARTYID3SP PARTYID4SP wt1SP basewtSP wtSP) update
drop if _merge==2
drop NOV2012VOTE 

drop _merge

recode religattendsp -1=.
recode IDEOL1SP -1=. 8=.

svyset [pweight=wt1SP],  singleunit(scaled)



*** Party ID

*** Init

gen partyid_start = -1 if PARTYID1SP==1
replace partyid_start = 1 if PARTYID1SP==2
replace partyid_start = -1 if PARTYID3SP==1
replace partyid_start = 1 if PARTYID3SP==2
replace partyid_start = -1 if PARTYID4SP==1
replace partyid_start = 1 if PARTYID4SP==2
replace partyid_start = 0 if PARTYID1SP==3 & partyid_start==.
replace partyid_start = 0 if PARTYID1SP==4 & partyid_start==.

gen dempartystrength_start = 0 if partyid_start!=-1 & partyid_start!=.
replace dempartystrength_start = 1 if partyid_start==-1 & PARTYID3SP==1
replace dempartystrength_start = 1 if partyid_start==-1 & PARTYID4SP==1
replace dempartystrength_start = 2 if partyid_start==-1 & PARTYID2SP==2
replace dempartystrength_start = 3 if partyid_start==-1 & PARTYID2SP==1

gen reppartystrength_start = 0 if partyid_start!=1 & partyid_start!=.
replace reppartystrength_start = 1 if partyid_start==1 & PARTYID3SP==2
replace reppartystrength_start = 1 if partyid_start==1 & PARTYID4SP==2
replace reppartystrength_start = 2 if partyid_start==1 & PARTYID2SP==2
replace reppartystrength_start = 3 if partyid_start==1 & PARTYID2SP==1

gen ideology_start = IDEOL1SP

** Oct 12

gen partyid_oct12 = -1 if PARTYID1S10==2
replace partyid_oct12 = 1 if PARTYID1S10==3
replace partyid_oct12 = -1 if PARTYID3S10==2
replace partyid_oct12 = 1 if PARTYID3S10==3
replace partyid_oct12 = -1 if PARTYID4S10==2
replace partyid_oct12 = 1 if PARTYID4S10==3
replace partyid_oct12 = 0 if PARTYID1S10==4 & partyid_oct12==.
replace partyid_oct12 = 0 if PARTYID1S10==5 & partyid_oct12==.

gen dempartystrength_oct12 = 0 if partyid_oct12!=-1 & partyid_oct12!=.
replace dempartystrength_oct12 = 1 if partyid_oct12==-1 & PARTYID3S10==2
replace dempartystrength_oct12 = 1 if partyid_oct12==-1 & PARTYID4S10==2
replace dempartystrength_oct12 = 2 if partyid_oct12==-1 & PARTYID2S10==3
replace dempartystrength_oct12 = 3 if partyid_oct12==-1 & PARTYID2S10==2

gen reppartystrength_oct12 = 0 if partyid_oct12!=1 & partyid_oct12!=.
replace reppartystrength_oct12 = 1 if partyid_oct12==1 & PARTYID3S10==3
replace reppartystrength_oct12 = 1 if partyid_oct12==1 & PARTYID4S10==3
replace reppartystrength_oct12 = 2 if partyid_oct12==1 & PARTYID2S10==3
replace reppartystrength_oct12 = 3 if partyid_oct12==1 & PARTYID2S10==2

gen approve_oct12 = APPRPRESS10
recode approve_oct12 1=. 6=.
replace approve_oct12 = approve_oct12-1

gen grpaffa_oct12 = GRPAFFS10_A
recode grpaffa_oct12 -1=.
gen grpaffb_oct12 = GRPAFFS10_B
recode grpaffb_oct12 -1=.
gen grpaffj_oct12 = GRPAFFS10_J
recode grpaffj_oct12 -1=.
gen grpaffk_oct12 = GRPAFFS10_K
recode grpaffk_oct12 -1=.

gen ideology_oct12=IDEOL1S10
recode ideology_oct12 1=. 9=.
replace ideology_oct12 = ideology_oct12-1

gen vote2012 = VOTE2012PS12
recode vote2012 2=1 3=0 4=. 1=.

** Sep 14

gen partyid_sep14 = -1 if PARTYID1S34==2
replace partyid_sep14 = 1 if PARTYID1S34==3
replace partyid_sep14 = -1 if PARTYID3S34==2
replace partyid_sep14 = 1 if PARTYID3S34==3
replace partyid_sep14 = -1 if PARTYID4S34==2
replace partyid_sep14 = 1 if PARTYID4S34==3
replace partyid_sep14 = 0 if PARTYID1S34==4 & partyid_sep14==.
replace partyid_sep14 = 0 if PARTYID1S34==5 & partyid_sep14==.

gen dempartystrength_sep14 = 0 if partyid_sep14!=-1 & partyid_sep14!=.
replace dempartystrength_sep14 = 1 if partyid_sep14==-1 & PARTYID3S34==2
replace dempartystrength_sep14 = 1 if partyid_sep14==-1 & PARTYID4S34==2
replace dempartystrength_sep14 = 2 if partyid_sep14==-1 & PARTYID2S34==3
replace dempartystrength_sep14 = 3 if partyid_sep14==-1 & PARTYID2S34==2

gen reppartystrength_sep14 = 0 if partyid_sep14!=1 & partyid_sep14!=.
replace reppartystrength_sep14 = 1 if partyid_sep14==1 & PARTYID3S34==3
replace reppartystrength_sep14 = 1 if partyid_sep14==1 & PARTYID4S34==3
replace reppartystrength_sep14 = 2 if partyid_sep14==1 & PARTYID2S34==3
replace reppartystrength_sep14 = 3 if partyid_sep14==1 & PARTYID2S34==2

tab partyid_sep14 PARTYID1S34
tab partyid_sep14 PARTYID2S34
tab partyid_sep14 PARTYID3S34
tab partyid_sep14 PARTYID4S34

gen approve_sep14 = APPRPRESS34
recode approve_sep14 1=. 6=.
replace approve_sep14 = approve_sep14-1


** Nov 14

gen partyid_nov14 = -1 if PARTYID1S36==2
replace partyid_nov14 = 1 if PARTYID1S36==3
replace partyid_nov14 = -1 if PARTYID3S36==2
replace partyid_nov14 = 1 if PARTYID3S36==3
replace partyid_nov14 = -1 if PARTYID4S36==2
replace partyid_nov14 = 1 if PARTYID4S36==3
replace partyid_nov14 = 0 if PARTYID1S36==4 & partyid_nov14==.
replace partyid_nov14 = 0 if PARTYID1S36==5 & partyid_nov14==.

gen dempartystrength_nov14 = 0 if partyid_nov14!=-1 & partyid_nov14!=.
replace dempartystrength_nov14 = 1 if partyid_nov14==-1 & PARTYID3S36==2
replace dempartystrength_nov14 = 1 if partyid_nov14==-1 & PARTYID4S36==2
replace dempartystrength_nov14 = 2 if partyid_nov14==-1 & PARTYID2S36==3
replace dempartystrength_nov14 = 3 if partyid_nov14==-1 & PARTYID2S36==2

gen reppartystrength_nov14 = 0 if partyid_nov14!=1 & partyid_nov14!=.
replace reppartystrength_nov14 = 1 if partyid_nov14==1 & PARTYID3S36==3
replace reppartystrength_nov14 = 1 if partyid_nov14==1 & PARTYID4S36==3
replace reppartystrength_nov14 = 2 if partyid_nov14==1 & PARTYID2S36==3
replace reppartystrength_nov14 = 3 if partyid_nov14==1 & PARTYID2S36==2

tab partyid_nov14 PARTYID1S36
tab partyid_nov14 PARTYID2S36
tab partyid_nov14 PARTYID3S36
tab partyid_nov14 PARTYID4S36

gen approve_nov14 = APPRPRESS36
recode approve_nov14 1=. 6=.
replace approve_nov14 = approve_nov14-1

** 2016

gen partyid_may16 = -1 if PARTYID1S54==2
replace partyid_may16 = 1 if PARTYID1S54==3
replace partyid_may16 = -1 if PARTYID3S54==2
replace partyid_may16 = 1 if PARTYID3S54==3
replace partyid_may16 = -1 if PARTYID4S54==2
replace partyid_may16 = 1 if PARTYID4S54==3
replace partyid_may16 = 0 if PARTYID1S54==4 & partyid_may16==.
replace partyid_may16 = 0 if PARTYID1S54==5 & partyid_may16==.

gen dempartystrength_may16 = 0 if partyid_may16!=-1 & partyid_may16!=.
replace dempartystrength_may16 = 1 if partyid_may16==-1 & PARTYID3S54==2
replace dempartystrength_may16 = 1 if partyid_may16==-1 & PARTYID4S54==2
replace dempartystrength_may16 = 2 if partyid_may16==-1 & PARTYID2S54==3
replace dempartystrength_may16 = 3 if partyid_may16==-1 & PARTYID2S54==2

gen reppartystrength_may16 = 0 if partyid_may16!=1 & partyid_may16!=.
replace reppartystrength_may16 = 1 if partyid_may16==1 & PARTYID3S54==3
replace reppartystrength_may16 = 1 if partyid_may16==1 & PARTYID4S54==3
replace reppartystrength_may16 = 2 if partyid_may16==1 & PARTYID2S54==3
replace reppartystrength_may16 = 3 if partyid_may16==1 & PARTYID2S54==2

tab partyid_may16 PARTYID1S54
tab partyid_may16 PARTYID2S54
tab partyid_may16 PARTYID3S54
tab partyid_may16 PARTYID4S54

gen approve_may16 = APPRPRESS54
recode approve_may16 1=. 6=.
replace approve_may16 = approve_may16-1

** Dec 16

gen partyid_dec16 = -1 if PARTYID1S61==2
replace partyid_dec16 = 1 if PARTYID1S61==3
replace partyid_dec16 = -1 if PARTYID3S61==2
replace partyid_dec16 = 1 if PARTYID3S61==3
replace partyid_dec16 = -1 if PARTYID4S61==2
replace partyid_dec16 = 1 if PARTYID4S61==3
replace partyid_dec16 = 0 if PARTYID1S61==4 & partyid_dec16==.
replace partyid_dec16 = 0 if PARTYID1S61==5 & partyid_dec16==.

gen dempartystrength_dec16 = 0 if partyid_dec16!=-1 & partyid_dec16!=.
replace dempartystrength_dec16 = 1 if partyid_dec16==-1 & PARTYID3S61==2
replace dempartystrength_dec16 = 1 if partyid_dec16==-1 & PARTYID4S61==2
replace dempartystrength_dec16 = 2 if partyid_dec16==-1 & PARTYID2S61==3
replace dempartystrength_dec16 = 3 if partyid_dec16==-1 & PARTYID2S61==2

gen reppartystrength_dec16 = 0 if partyid_dec16!=1 & partyid_dec16!=.
replace reppartystrength_dec16 = 1 if partyid_dec16==1 & PARTYID3S61==3
replace reppartystrength_dec16 = 1 if partyid_dec16==1 & PARTYID4S61==3
replace reppartystrength_dec16 = 2 if partyid_dec16==1 & PARTYID2S61==3
replace reppartystrength_dec16 = 3 if partyid_dec16==1 & PARTYID2S61==2

tab partyid_dec16 PARTYID1S61
tab partyid_dec16 PARTYID2S61
tab partyid_dec16 PARTYID3S61
tab partyid_dec16 PARTYID4S61

svy: tab partyid_may16 partyid_oct12, cell
svy: tab partyid_dec16 partyid_oct12, cell
svy: tab partyid_dec16 partyid_may16, cell

gen approve_dec16 = APPRPRESS61
recode approve_dec16 1=. 6=.
replace approve_dec16 = approve_dec16-1

tab VOTE2016PS60
gen vote2016_nov = VOTE2016PS60
recode vote2016_nov 1=. 2=1 3=0 4/7=.

tab VOTE2016PS61
gen vote2016_dec = VOTE2016PS61
recode vote2016_dec 1=. 2=1 3=0 4/5=.

drop PARTYID1S* PARTYID2S* PARTYID3S* PARTYID4S*
drop APPRPRES* IDEOL* GRPAFF* PARTYIDINSERTS
drop wtSP basewtSP


tab partyid_nov14 merge2014
tab partyid_nov14 merge2012




drop VOTE2012PS12 VOTE2016P* 

recode partyid_start -1=0 0=1 1=2
recode partyid_oct12 -1=0 0=1 1=2
recode partyid_sep14 -1=0 0=1 1=2
recode partyid_nov14 -1=0 0=1 1=2
recode partyid_may16 -1=0 0=1 1=2
recode partyid_dec16 -1=0 0=1 1=2

gen partyid_7pt_start = 0 if partyid_start==0 & dempartystrength_start==3
replace partyid_7pt_start = 1 if partyid_start==0 & dempartystrength_start==2
replace partyid_7pt_start = 2 if partyid_start==0 & dempartystrength_start==1
replace partyid_7pt_start = 3 if partyid_start==1
replace partyid_7pt_start = 4 if partyid_start==2 & reppartystrength_start==1
replace partyid_7pt_start = 5 if partyid_start==2 & reppartystrength_start==2
replace partyid_7pt_start = 6 if partyid_start==2 & reppartystrength_start==3

gen partyid_7pt_oct12 = 0 if partyid_oct12==0 & dempartystrength_oct12==3
replace partyid_7pt_oct12 = 1 if partyid_oct12==0 & dempartystrength_oct12==2
replace partyid_7pt_oct12 = 2 if partyid_oct12==0 & dempartystrength_oct12==1
replace partyid_7pt_oct12 = 3 if partyid_oct12==1
replace partyid_7pt_oct12 = 4 if partyid_oct12==2 & reppartystrength_oct12==1
replace partyid_7pt_oct12 = 5 if partyid_oct12==2 & reppartystrength_oct12==2
replace partyid_7pt_oct12 = 6 if partyid_oct12==2 & reppartystrength_oct12==3

gen partyid_7pt_sep14 = 0 if partyid_sep14==0 & dempartystrength_sep14==3
replace partyid_7pt_sep14 = 1 if partyid_sep14==0 & dempartystrength_sep14==2
replace partyid_7pt_sep14 = 2 if partyid_sep14==0 & dempartystrength_sep14==1
replace partyid_7pt_sep14 = 3 if partyid_sep14==1
replace partyid_7pt_sep14 = 4 if partyid_sep14==2 & reppartystrength_sep14==1
replace partyid_7pt_sep14 = 5 if partyid_sep14==2 & reppartystrength_sep14==2
replace partyid_7pt_sep14 = 6 if partyid_sep14==2 & reppartystrength_sep14==3

gen partyid_7pt_nov14 = 0 if partyid_nov14==0 & dempartystrength_nov14==3
replace partyid_7pt_nov14 = 1 if partyid_nov14==0 & dempartystrength_nov14==2
replace partyid_7pt_nov14 = 2 if partyid_nov14==0 & dempartystrength_nov14==1
replace partyid_7pt_nov14 = 3 if partyid_nov14==1
replace partyid_7pt_nov14 = 4 if partyid_nov14==2 & reppartystrength_nov14==1
replace partyid_7pt_nov14 = 5 if partyid_nov14==2 & reppartystrength_nov14==2
replace partyid_7pt_nov14 = 6 if partyid_nov14==2 & reppartystrength_nov14==3

gen partyid_7pt_may16 = 0 if partyid_may16==0 & dempartystrength_may16==3
replace partyid_7pt_may16 = 1 if partyid_may16==0 & dempartystrength_may16==2
replace partyid_7pt_may16 = 2 if partyid_may16==0 & dempartystrength_may16==1
replace partyid_7pt_may16 = 3 if partyid_may16==1
replace partyid_7pt_may16 = 4 if partyid_may16==2 & reppartystrength_may16==1
replace partyid_7pt_may16 = 5 if partyid_may16==2 & reppartystrength_may16==2
replace partyid_7pt_may16 = 6 if partyid_may16==2 & reppartystrength_may16==3

gen partyid_7pt_dec16 = 0 if partyid_dec16==0 & dempartystrength_dec16==3
replace partyid_7pt_dec16 = 1 if partyid_dec16==0 & dempartystrength_dec16==2
replace partyid_7pt_dec16 = 2 if partyid_dec16==0 & dempartystrength_dec16==1
replace partyid_7pt_dec16 = 3 if partyid_dec16==1
replace partyid_7pt_dec16 = 4 if partyid_dec16==2 & reppartystrength_dec16==1
replace partyid_7pt_dec16 = 5 if partyid_dec16==2 & reppartystrength_dec16==2
replace partyid_7pt_dec16 = 6 if partyid_dec16==2 & reppartystrength_dec16==3

*** Observed rates


gen candprefswitch = 0 if vote2012!=. & vote2016_nov!=.
replace candprefswitch = 1 if vote2012==0 & vote2016_nov==1
replace candprefswitch = 1 if vote2012==1 & vote2016_nov==0
svy: proportion candprefswitch


gen partyidswitchdec = 0 if partyid_7pt_oct12!=. & partyid_7pt_dec16!=.
replace partyidswitchdec = 1 if partyid_7pt_oct12<=2 & partyid_7pt_dec16>=4 & partyid_7pt_dec16<7
replace partyidswitchdec = 1 if partyid_7pt_oct12>=4 & partyid_7pt_oct12<7 & partyid_7pt_dec16<=2
svy: proportion partyidswitchdec

tab candprefswitch partyidswitchdec

tab merge2014 if panel2012==1

tab merge2014 if panel2012==1 & partyidswitch==.

drop candprefswitch partyidswitchdec


*** Now for Multiple Imputation

mi set mlong
mi register imputed religattend vote2012 vote2016_nov vote2016_dec ideology_start partyid_7pt_start  partyid_7pt_oct12  partyid_7pt_sep14 partyid_7pt_nov14 partyid_7pt_may16 partyid_7pt_dec16 grpaffa_oct12 grpaffb_oct12 grpaffj_oct12 grpaffk_oct12
mi misstable summarize 

mi impute chained (ologit, ascontinuous) religattend ideology_start grpaffa_oct12  grpaffb_oct12 grpaffj_oct12 grpaffk_oct12 (ologit) partyid_7pt_start  partyid_7pt_oct12  partyid_7pt_sep14 partyid_7pt_nov14 partyid_7pt_may16 partyid_7pt_dec16 (logit, omit(i.partyid_7pt_start i.partyid_7pt_sep14 i.partyid_7pt_nov14 i.partyid_7pt_dec16 i.partyid_7pt_may16)) vote2012 (logit, omit(i.partyid_7pt_start i.partyid_7pt_nov14 i.partyid_7pt_dec16 i.partyid_7pt_sep14 i.partyid_7pt_oct12)) vote2016_nov = i.ppgender i.ppethm i.ppeducat i.ppreg4 i.ppagect4, add(40) burnin(20) rseed(998877) chaindots 

mi passive: gen partyidswitchstart = 0 if partyid_7pt_start!=. & partyid_7pt_may16!=.
mi passive: replace partyidswitch = 1 if partyid_7pt_start<=2 & partyid_7pt_may16>=4 & partyid_7pt_may16<7
mi passive: replace partyidswitch = 1 if partyid_7pt_start>=4 & partyid_7pt_oct12<7 & partyid_7pt_may16<=2
mi estimate: svy: proportion partyid_7pt_start partyid_7pt_may16 partyidswitch


mi passive: gen partyidswitchoct = 0 if partyid_7pt_oct12!=. & partyid_7pt_may16!=.
mi passive: replace partyidswitchoct = 1 if partyid_7pt_oct12<=2 & partyid_7pt_may16>=4 & partyid_7pt_may16<7
mi passive: replace partyidswitchoct = 1 if partyid_7pt_oct12>=4 & partyid_7pt_oct12<7 & partyid_7pt_may16<=2
mi estimate: svy: proportion partyid_7pt_oct12 partyid_7pt_may16 partyidswitchoct


mi passive: gen partyidswitchdec = 0 if partyid_7pt_oct12!=. & partyid_7pt_dec16!=.
mi passive: replace partyidswitchdec = 1 if partyid_7pt_oct12<=2 & partyid_7pt_dec16>=4 & partyid_7pt_dec16<7
mi passive: replace partyidswitchdec = 1 if partyid_7pt_oct12>=4 & partyid_7pt_oct12<7 & partyid_7pt_dec16<=2
mi estimate: svy: proportion partyid_7pt_oct12 partyid_7pt_dec16 partyidswitchdec


mi passive: gen candprefswitch = 0 if vote2012!=. & vote2016_nov!=.
mi passive: replace candprefswitch = 1 if vote2012==0 & vote2016_nov==1
mi passive: replace candprefswitch = 1 if vote2012==1 & vote2016_nov==0
mi estimate: svy: proportion candprefswitch

tab partyid_oct12 partyid_may16 if _mi_m==0 [iw=wt1SP], cell

mi estimate: svy: proportion partyidswitchdec, over(candprefswitch)
mi estimate: svy: proportion candprefswitch, over(partyidswitchdec)

mi estimate: svy: proportion partyidswitchdec, over(partyid_7pt_oct12)

mi estimate: svy: proportion partyidswitchdec, over(ppeducat)



